﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Configuration;

namespace WebApp.Common
{
    public class AESBase64
    {
        //默认密钥向量   
        private static byte[] _iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        //private static byte[] _iv = null;

        public static string Encrypt(string plainText)
        {
            try
            {
                //key必须16位
                string encryptKey = ConfigurationManager.AppSettings["aes.encrypt.key"];
                if (string.IsNullOrEmpty(encryptKey))
                {
                    encryptKey = "we#win$WY$201706";
                }

                return AESEncrypt(plainText, encryptKey);
            }
            catch (Exception ex)
            {
                //Log.Error(ex);
                return plainText;
            }
        }

        public static string Decrypt(string cipherText)
        {
            try
            {
                //key必须16位
                string encryptKey = ConfigurationManager.AppSettings["aes.encrypt.key"];
                if (string.IsNullOrEmpty(encryptKey))
                {
                    encryptKey = "we#win$WY$201706";
                }

                return AESDecrypt(cipherText, encryptKey);
            }
            catch (Exception ex)
            {
                //Log.Error(ex);
                return cipherText;
            }
        }

        /// <summary>          
        /// AES加密         
        /// </summary>          
        /// <param name="inputdata">输入的数据</param>         
        /// <param name="iv">向量128位</param>         
        /// <param name="strKey">加密密钥</param>         
        /// <returns></returns>          
        public static string AESEncrypt(string plainText, string strKey)
        {
            if (string.IsNullOrEmpty(plainText)) return string.Empty;

            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 256;

            rijndaelCipher.BlockSize = 128;

            byte[] pwdBytes = (new SHA256Managed()).ComputeHash(Encoding.UTF8.GetBytes(strKey));

            rijndaelCipher.Key = pwdBytes;


            rijndaelCipher.IV = _iv;

            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

            byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);

            byte[] cipherBytes = transform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);

            return Convert.ToBase64String(cipherBytes);
        }

        /// <summary>         
        /// AES解密         
        /// </summary> 
        /// <param name="inputdata">输入的数据</param>         
        /// <param name="iv">向量128</param>         
        /// <param name="strKey">key</param>         
        /// <returns></returns> 
        public static string AESDecrypt(string cipherText, string strKey)
        {
            if (string.IsNullOrEmpty(cipherText)) return string.Empty;

            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 256;
            rijndaelCipher.BlockSize = 128;
            rijndaelCipher.FeedbackSize = 128;
            byte[] encryptedData = Convert.FromBase64String(cipherText);
            byte[] pwdBytes = (new SHA256Managed()).ComputeHash(Encoding.UTF8.GetBytes(strKey));  
            rijndaelCipher.Key = pwdBytes;
            rijndaelCipher.IV = _iv;

            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

            return Encoding.UTF8.GetString(plainText);
        }
    }

}
